package com.itheima.dao.impl;

import com.itheima.dao.IMyDao;
import com.itheima.domain.My;
import com.itheima.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class MyDaoImpl implements IMyDao {
    private QueryRunner runner;
    private ConnectionUtils connectionUtils;

    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    public List<My> findAll() {
        try{
            return runner.query(connectionUtils.getThreadConnection(),"select * from my",new BeanListHandler<My>(My.class));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void updateById(Integer id,Integer id2) {
        try{
             runner.update(connectionUtils.getThreadConnection(),"update my set t2 =t2 + ? where t1= ?", id ,id2 );

        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
